#!C:\strawberry\perl

$numArgs = $#ARGV + 1;

if ($numArgs == 2) {	
	$filePath = "$ARGV[0]";
	$outputName = "$ARGV[1]";
	open LEVEL, $filePath or die $!;
	$height = 0;
	$length = 0;
	$xmlheader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<XnaContent>\n<Asset>\n";
	$xmlequiv = "";
	while (<LEVEL>) {
		#remove excess whitespace and \n`s
		chomp;
		
		$xmlequiv .= "<Row>\n";
		
		if ((length "$_") > $length) {
			$length = length "$_";
		}
		
		#generate xml for this tile
		for (my $i = 0; $i < length($_); $i++) {
			$letter = substr $_, $i, 1;
			if ($letter eq "g") {
				$xmlequiv .= "<Ground>\n<X>$i</X>\n<Y>$height</Y>\n</Ground>\n";
			}
			elsif ($letter eq "m") {
				$xmlequiv .= "<Dirt>\n<X>$i</X>\n<Y>$height</Y>\n</Ground>\n";
			}
			elsif ($letter eq "w") {
				$xmlequiv .= "<Win>\n<X>$i</X>\n<Y>$height</Y>\n</Win>\n";
			}
			elsif ($letter eq "d") {
				$xmlequiv .= "<Dead>\n<X>$i</X>\n<Y>$height</Y>\n</Dead>\n";
			}
		}
		
		$xmlequiv .= "</Row>\n";
		$height++;
	}

	close(LEVEL);
	
	$xmlheader .= "<Length>$length</Length>\n<Height>$height</Height>\n";
	$xmlequiv = $xmlheader.$xmlequiv."</Asset>\n</XnaContent>\n";
	
	#fix output
	open(XML, ">$outputName");
	printf XML $xmlequiv;
	close (XML);
}